<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/model/DAO/DepartmentTypeDAO.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/Entity.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/entities/DepartmentType.php';
require_once $_SERVER['DOCUMENT_ROOT'].'/exceptions/DataBaseException.php';

/**
 * Класс, отвечающий за работу с типами структурных подразделений в БД
 */
class DepartmentTypeDAOImpl extends DepartmentTypeDAO {

	/**
	 * Метод, создающий в БД новую запись типа структурного подразделения
	 */
	public function create(Entity $departmentType) {
/*		$sql = 'INSERT INTO `department_type` (`name`,`id_post`) ';
		$sql .= 'VALUES ("'.$departmentType->getName().'","'.$departmentType->getIdPost().'")';
		try {
			$this->query($sql);
			$departmentType->setIdentity($this->identity());
			return $departmentType;
		} catch(DataBaseException $exception) {
			return null;
		}
*/

		$name = $departmentType->getName();
		$postId = $departmentType->getIdPost();
		
		$stmt = $this->dbh->connection->prepare('INSERT INTO `department_type` (`name`, `id_post`) VALUES (?, ?)');
		$stmt->bindValue(1, $name);
		$stmt->bindValue(2, $postId);
		
		try {
			$this->dbh->query($stmt);
			$departmentType->setIdentity($this->dbh->identity());
			return $departmentType;
		} catch (PDOException $exception) {
			return null;
		}
		
	}

	/**
	 * Метод, получающий запись с типом структурного подразделения из БД
	 */
	public function read($identity) {
/*		$sql = 'SELECT `id`,`name`, `id_post`';
		$sql .= 'FROM `department_type` ';
		$sql .= 'WHERE `id`='.((int)$identity);
		$this->query($sql);
		$departmentType = null;
		if($row = $this->row()) {
			$departmentType = new DepartmentType();
			$departmentType->setIdentity($row['id']);
			$departmentType->setName($row['name']);
			$departmentType->setIdPost($row['id_post']);
		}
		$this->close();
		return $departmentType;
*/

		$id = ((int)$identity);
				
		$stmt = $this->dbh->connection->prepare('SELECT `id`, `name`, `id_post` FROM `department_type` WHERE `id`=?');
		$stmt->bindValue(1, $id);
		
		$this->dbh->query($stmt);
		$departmentType = null;
		if($row = $this->dbh->row()) {
			$departmentType = new DepartmentType();
			$departmentType->setIdentity($row['id']);
			$departmentType->setName($row['name']);
			$departmentType->setIdPost($row['id_post']);
		}
		$this->dbh->close();
		return $departmentType;
		
	}
	
	/**
	 * Метод, обновляющий запись типа структуного подразделения в БД
	 */
	public function update(Entity $departmentType) {
/*		$sql = 'UPDATE `department_type` ';
		$sql .= 'SET `name`="'.$name = $departmentType->getName().'"'.',`id_post`="'.$name = $departmentType->getIdPost().'" ';
		$sql .= 'WHERE `id`='.$departmentType->getIdentity();
		try {
			$this->query($sql);
			return $departmentType;
		} catch(DataBaseException $exception) {
			return null;
		}
*/

		$name = $departmentType->getName();
		$postId = $departmentType->getIdPost();
		$id = $process->getIdentity();
		
		$stmt = $this->dbh->connection->prepare('UPDATE `department_type` SET `name`=?, `id_post`=? WHERE `id`=?');
		$stmt->bindValue(1, $name);
		$stmt->bindValue(2, $postId);
		$stmt->bindValue(3, $id);
		
		try {
			$this->dbh->query($stmt);
			return $departmentType;
		} catch (PDOException $exception) {
			return null;
		}
		
	}
	
	/**
	 * Метод, удаляющий запись типа структуного подразделения из БД
	 */
	public function delete($identity) {
/*		$sql = 'DELETE FROM `department_type` ';
		$sql .= 'WHERE `id`='.((int)$identity);
		try {
			$this->query($sql);
			return true;
		} catch(DataBaseException $exception) {
			return false;
		}
*/

		$id = (int)($identity);
		
		$stmt = $this->dbh->connection->prepare('DELETE FROM `department_type` WHERE `id`=?');
		$stmt->bindValue(1, $id);
		
		try {
			$this->dbh->query($stmt);
			return true;
		} catch (PDOException $exception) {
			return false;
		}
		
	}

	/**
	 * Метод, получающий все записи типов структурных подразделений из БД
	 */
	public function getAll() {
/*		$departmentTypes = array();
		$sql = 'SELECT `id`,`name`, `id_post` ';
		$sql .= 'FROM `department_type`';
		$this->query($sql);
		while($row = $this->row()) {
			$departmentType = new DepartmentType();
			$departmentType->setIdentity($row['id']);
			$departmentType->setName($row['name']);
			$departmentType->setIdPost($row['id_post']);
			$departmentTypes[] = $departmentType;
		}
		$this->close();
		return $departmentTypes;
*/

		$departmentTypes = array();
		$stmt = $this->dbh->connection->prepare('SELECT `id`, `name`, `id_post` FROM `department_type`');		
		$this->dbh->query($stmt);
		while($row = $this->dbh->row()) {
			$departmentType = new DepartmentType();
			$departmentType->setIdentity($row['id']);
			$departmentType->setName($row['name']);
			$departmentType->setIdPost($row['id_post']);
			$departmentTypes[] = $departmentType;
		}
		$this->dbh->close();
		return $departmentTypes;			
	}
}
?>